package com.thfw.test.view.activity;

import com.thfw.test.view.ViseBle;
import com.thfw.test.view.adapter.BlueToothAdapter;
import com.thfw.test.view.adapter.WifiListAdapter;
import com.thfw.test.view.callback.scan.IScanCallback;
import com.thfw.test.view.callback.scan.ScanCallback;
import com.thfw.test.view.model.BluetoothLeDevice;
import com.thfw.test.view.model.BluetoothLeDeviceStore;
import com.vise.log.ViseLog;

import java.util.ArrayList;

/**
 * Created BY Cuishikun
 * Created ON 2021/9/14
 * Package Name com.thfw.test.view.activity
 **/
public class BlueToothConnectActivity extends BaseActivity {

    /**
     * 蓝牙模块
     */
    private BluetoothLeDeviceStore bluetoothLeDeviceStore = new BluetoothLeDeviceStore();
    private BlueToothAdapter adapter;
    private boolean isLoading;

    @Override
    public int getLayoutId() {
        return 0;
    }

    @Override
    public void initView() {

    }


        /**
     * 扫描回调
     */
    private ScanCallback periodScanCallback = new ScanCallback(new IScanCallback() {
        @Override
        public void onDeviceFound(final BluetoothLeDevice bluetoothLeDevice) {
            ViseLog.i("Founded Scan Device:" + bluetoothLeDevice);
            bluetoothLeDeviceStore.addDevice(bluetoothLeDevice);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (adapter != null && bluetoothLeDeviceStore != null) {
                        adapter.setListAll(bluetoothLeDeviceStore.getDeviceList());
                    }
                }
            });
        }

        @Override
        public void onScanFinish(BluetoothLeDeviceStore bluetoothLeDeviceStore) {
            ViseLog.i("scan finish " + bluetoothLeDeviceStore);
        }

        @Override
        public void onScanTimeout() {

            ViseLog.i("scan timeout");
        }

    });


    /**
     * 开始扫描
     */
    private void startScan() {

        if (adapter != null) {
            adapter.setListAll(new ArrayList<BluetoothLeDevice>());
        }
        ViseBle.getInstance().startScan(periodScanCallback);
        invalidateOptionsMenu();
        isLoading = true;
    }

    /**
     * 停止扫描
     */
    private void stopScan() {
        ViseBle.getInstance().stopScan(periodScanCallback);
        invalidateOptionsMenu();
        isLoading = false;
    }
}
